<?php

class Customer extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('customer_model');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		//$this->load->library('security');
		$this->load->library('auth/tank_auth');
		$this->lang->load('auth/tank_auth');
		$this->load->model('tank_auth/users');
		$this->load->library('pagination');
		$this->db_front = $this->load->database('front', TRUE);
	}
	
	function index(){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			if ($this->tank_auth->get_url()){
				$offset = $this->uri->segment(3);
				$limit = '10';
				if(!$offset){
					$page = 0;
				}else{
					$page = $offset; 	
				}
				
				$data['hasil'] = $this->customer_model->getAll($page,$limit);
				
				$config_pagination['base_url']    = site_url('customer/index');
				$config_pagination['total_rows']  = count($this->customer_model->getAll('0','0'));
				$config_pagination['per_page']    = $limit;
				$config_pagination['uri_segment'] = 3;
				$config_pagination['num_links']   = 4;
		
				//pagination style
				$config_pagination['full_tag_open'] = '<div class="pagination"><ul>';
				$config_pagination['full_tag_close'] = '</ul></div>';
		
				$config_pagination['first_link'] = 'First';
				$config_pagination['first_tag_open']  = '<li>';
				$config_pagination['first_tag_close'] = '</li>';
		
				$config_pagination['last_link'] = 'Last';
				$config_pagination['last_tag_open']  = '<li>';
				$config_pagination['last_tag_close'] = '</li>';
		
				$config_pagination['next_link'] = '&gt;';
				$config_pagination['next_tag_open']  = '<li>';
				$config_pagination['next_tag_close'] = '</li>';
		
				$config_pagination['prev_link'] = '&lt;';
				$config_pagination['prev_tag_open'] = '<li>';
				$config_pagination['prev_tag_close'] = '</li>';
		
				$config_pagination['cur_tag_open'] = '<li class="disabled"><a href="#">';
				$config_pagination['cur_tag_close'] = '</a></li>';
		
				$config_pagination['num_tag_open'] = '<li>';
				$config_pagination['num_tag_close'] = '</li>';
		
				$this->pagination->initialize($config_pagination); 
		
				$data['pagination'] = $this->pagination->create_links();
						
				
				$data['content'] = 'customer';
				$this->load->view('common/common',$data);
			}else{
				redirect('/common/');
			}
		}
	}
	
	function updatedata($id = ''){
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login');
			}
		else {
			if($id){
				 $id_user = $this->input->post('id_user',TRUE);
				 //$user = $this->users->get_user_by_id($id,$activated);
				 $user = $this->users->select_customer($id);
				 //var_dump($user);
				 $data['id_user'] = $user->id;
				 $data['username'] = $user->username;
				 $data['email'] = $user->email;
				 $password = $user->password;
				 $data['activated'] = $user->activated;
				 $data['banned'] = $user->banned;
				 $data['ban_reason'] = $user->ban_reason;
				 $ban_reason = $user->ban_reason;
				 $data['update'] = TRUE;
				 
			 	
				}
				else{
					redirect('/auth/register/');					
				}
				
		
			$use_username = $this->config->item('use_username', 'tank_auth');
			if ($use_username) {
				$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|max_length['.$this->config->item('username_max_length', 'tank_auth').']|alpha_dash');
			}
			$this->form_validation->set_rules('email', 'Email', 'trim|xss_clean|valid_email');
			
			if($this->input->post('password',TRUE)){
			$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
			$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');
			}
			
			$this->form_validation->set_message('required', '<span style="color:#E91510"> %s is required </span>');
			$this->form_validation->set_message('matches', '<span style="color:#E91510"> %s must be equal to password</span>');
				
			$this->form_validation->set_rules('activated', 'Active', 'trim|required|xss_clean');
			$this->form_validation->set_rules('banned', 'No', 'trim|required|xss_clean');
			$this->form_validation->set_rules('ban_reason', 'trim|xss_clean');
            if ($this->tank_auth->get_url()){
				if ($this->form_validation->run()) {
						$this->users->update_customer($id_user,$this->form_validation->set_value('username'),$this->form_validation->set_value('email'),$this->form_validation->set_value('activated'),$this->form_validation->set_value('banned'),$this->form_validation->set_value('ban_reason'));
						
						if($this->form_validation->set_value('password') != ''){
								$hasher = new PasswordHash(
								$this->config->item('phpass_hash_strength', 'tank_auth'),
								$this->config->item('phpass_hash_portable', 'tank_auth'));
								$hashed_password = $hasher->HashPassword($this->form_validation->set_value('password'));

								//$data_password = array(
								//	'password' => $hashed_password
								//);
								
								$this->users->change_password_customer($id_user,$hashed_password);
						}
						//if($this->form_validation->set_value('password') != ''){
						//$this->tank_auth->change_password($id_user,$this->form_validation->set_value('password'));
						//echo 'passs';
						
						
						//} 
						redirect('/customer/index');
						exit;					
									
				}
			}else{
				redirect('/common/');
			}
			
			
			//$data['content'] = $this->load->view('auth/form_edit_user', $data,TRUE);			
			//$this->load->view('auth/edit_user_form', $data);
			$data['use_username'] = $use_username;
			$data['content'] = 'customer/customer_form_update';
      		$this->load->view('common/common',$data);
			
		}
    }
	
	function deletedata($id){
		if (!$this->tank_auth->is_logged_in())	redirect('/auth/login/');
		if ($this->tank_auth->get_url()){
			$this->users->delete_customer($id);
			$this->customer_model->delete_store($id);
			redirect('/customer/index');
		}else{
				redirect('/common/');
		}	
    }
	
}
?>